import java.util.Arrays;

public class Test {
    public static void bubbleSort2(int[] array) {//冒泡法02，将数列从小到大排列
        //优化：原代码需要走array,length趟，如果在这期间就已经有序的话，之后就不用再比较了。
        for (int i = 0;i < array.length-1;i++){//控制外层，走几趟
            boolean fle = false;
            for (int j = 0;j < array.length-1-i;j++){
                if (array[j] > array[j + 1]){
                    int tmp = array[j];
                    array[j] = array[j+1];
                    array[j + 1] = tmp;
                    fle = true;
                }
                if (fle = false){//如果fle仍为false，说明未执行上述元素调换，可知数列已经排列好了，之后就不再执行了
                    return;
                }
            }
        }
    }

    public static void main(String[] args) {
        int[] array = {2,4,16,18,10};
        bubbleSort2(array);
        System.out.println(Arrays.toString(array));
    }
    public static void bubbleSort(int[] array) {//冒泡法，将数列从小到大排列
        for (int i = 0;i < array.length-1;i++){//控制外层，走几趟
            for (int j = 0;j < array.length-1-i;j++){
                if (array[j] > array[j + 1]){
                    int tmp = array[j];
                    array[j] = array[j+1];
                    array[j + 1] = tmp;
                }
            }
        }
    }

    public static void main5(String[] args) {
        int[] array = {2,4,16,18,10};
        bubbleSort(array);
        System.out.println(Arrays.toString(array));
    }
    public static int binarySearch(int[] array,int key){// 第二个二分法使用,返回指定数字的顺序
        int left = 0;
        int right = array.length - 1;
        while (left <= right){
            int mid = (left + right)/2;
            if (array[mid] < key){
                left = mid + 1;
            }else if(array[mid] > key){
                right = mid -1;
            }else{
                return mid;
            }
        }
        return -1;//输入数列中不存在的数会返回-1
    }

    public static void main4(String[] args) {
        int[] array = {2,4,16,18,10};
        Arrays.sort(array);//对数组中的元素排序
        System.out.println(binarySearch(array,25));
    }
    public static int dichotomize(int[] array,int n) {//使用二分法，返回一个数列中指定数的下标
         int left = 0;
         int right = array.length-1;
         int mid = (left + right)/2;
         while (array[mid] != n) {
             if (n < array[mid]) {
                 right = mid;
             } else if (n > array[mid]) {
                 left = mid;
             }
             mid = (left + right)/2;
         }
         return mid;
    }

    public static void main3(String[] args) {
        int[] array = {1,2,3,4,5,6};
        int a = dichotomize(array,3);
        System.out.println(a);
    }
    public static void permute(int[] array) {//奇数偶数换位置
        int left = 0;
        int right = array.length-1;
        while (left < right) {
            while (left < right && array[left] % 2 != 0) {//遇偶则停
                left++;
            }
            while (left < right && array[right] % 2 == 0) {//遇奇则停
                right--;
            }
            int mid = array[left];
            array[left] = array[right];
            array[right] = mid;
        }
    }

    public static void main2(String[] args) {
        int[] array = {2,2,2,3,3,3};
        permute(array);
        System.out.println(Arrays.toString(array));
    }
    public static void transform(int[] array) {// 将数组中每个元素乘以2
        for (int i = 0;i < array.length;i++){
            array[i] = array[i]*2;
        }
    }

    public static void main1(String[] args) {
        int[] array = new int[]{1,2,3,4,5};
        transform(array);
        System.out.println(Arrays.toString(array));
    }
}
